#include "MprpcApplication.h"

#include <iostream>
#include <string>
#include <unistd.h>

MprpcConfig MprpcApplication::m_config;

MprpcConfig& MprpcApplication::GetConfig()
{
    return m_config;
}

void ShowArgsHelp(std::string str)
{
    std::cout << "format: "<<str<<" -i <configfile>" << std::endl;
}

void MprpcApplication::Init(int argc, char *argv[])
{
    if (argc < 2)
    {
        ShowArgsHelp(argv[0]);
        exit(EXIT_FAILURE);
    }

    int c = 0;
    std::string config_file;
    while ((c = getopt(argc, argv, "i:")) != -1)    // 解析命令行输入
    {
        switch (c)
        {
        case 'i':
            config_file = optarg;   // 获取命令行输入的参数
            break;
        case '?':
            ShowArgsHelp(argv[0]);
            exit(EXIT_FAILURE);
        case ':':
            exit(EXIT_FAILURE);
        default:
            break;
        }
    }

    // 加载配置文件 
    m_config.LoadConfig(config_file.c_str());
    // std::cout<<"rpcServerIp: "<<m_config.Load("rpcServerIp")<<std::endl;
    // std::cout<<"rpcServerPort: "<<m_config.Load("rpcServerPort")<<std::endl;
    // std::cout<<"zookeeperIp: "<<m_config.Load("zookeeperIp")<<std::endl;
    // std::cout<<"zookeeperPost: "<<m_config.Load("zookeeperPort")<<std::endl;
}

MprpcApplication &MprpcApplication::GetInstance()
{
    static MprpcApplication app;
    return app;
}